﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>FeedSync Test</title>
    <script type="text/javascript" src="MiscellaneousHelpers.js"></script>
    <script type="text/javascript" src="FeedSyncHelpers.js"></script>
    <script type="text/javascript">
        function Initialize()
            {
            var g_Feed = "<?xml version='1.0' encoding='utf-8'?>";
            g_Feed += " <rss xmlns:sx='http://feedsync.org/2007/feedsync' version='2.0'>";
            g_Feed += "  <channel>";
            g_Feed += "   <title>todolist.xml</title>";
            g_Feed += "   <description>Sample to do list</description>";
            g_Feed += "   <link>http://www.example.com/index.html</link>";
            g_Feed += "   <item>";
            g_Feed += "    <title>Pay Bills</title>";
            g_Feed += "    <description>Electric, Gas and Water Bills</description>";
            g_Feed += "    <sx:sync id='101' updates='1' deleted='false' noconflicts='false'>";
            g_Feed += "     <sx:history sequence='1' when='2007-11-29T00:27:11Z' by='fsRSSConvert.js' />";
            g_Feed += "    </sx:sync>";
            g_Feed += "   </item>";
            g_Feed += "  </channel>";
            g_Feed += " </rss>";

            var XMLDOMDocument1 = null;
            var XMLDOMDocument2 = null;
            var FeedSyncItemCollection1 = null;
            var FeedSyncItemCollection2 = null;
            var FeedSyncItem = null;
            var DescriptionXMLDOMNode = null;

            var XSLDOMDocument = LoadXML("xml-pretty-print.xsl"); 
            
            //  ----------------------------------------------------------------------
            //  BEGIN TEST 1 - Create new nodes
            //  ----------------------------------------------------------------------
            try
                {
                XMLDOMDocument1 = ParseXML(g_Feed, true);
                FeedSyncItemCollection1 = new FeedSyncItemCollectionClass(XMLDOMDocument1);

                FeedSyncItem = FeedSyncItemCollection1.CreateItem("ep1.100", "Buy groceries", "Get milk and eggs", "EP1");
                FeedSyncItemCollection1.AddItem(FeedSyncItem);

                FeedSyncItem = FeedSyncItemCollection1.CreateItem("ep1.101", "Get car serviced", "Needs oil change", "EP1");
                FeedSyncItemCollection1.AddItem(FeedSyncItem);

                XMLDOMDocument2 = ParseXML(g_Feed, true);
                FeedSyncItemCollection2 = new FeedSyncItemCollectionClass(XMLDOMDocument2);

                FeedSyncItem = FeedSyncItemCollection2.CreateItem("ep2.100", "Return books to library", "2 books in total", "EP2");
                FeedSyncItemCollection2.AddItem(FeedSyncItem);

                FeedSyncItem = FeedSyncItemCollection2.CreateItem("ep2.101", "Get birthday gift for mother", "She likes blue flowers", "EP2");
                FeedSyncItemCollection2.AddItem(FeedSyncItem);
                
                FeedSyncItemCollection1.MergeItems(FeedSyncItemCollection2);

                if (IsFirefox())
                    {
                    var TestDivHTMLDOMElement = document.getElementById("Test1");
                    SetTextForHTMLDOMNode(TestDivHTMLDOMElement, GetXML(FeedSyncItemCollection1.m_XMLDOMDocument));
                    }
                else
                    {             
                    document.write("<div><b><u>Test 1 Results</u></b></div>");
                    document.write(FeedSyncItemCollection1.m_XMLDOMDocument.transformNode(XSLDOMDocument));
                    document.write("<br/>");
                    }
                }
            catch (e)
                {
                alert("Exception in Test 1 - " + e.message);
                }
            //  ----------------------------------------------------------------------                
            //  END TEST 1
            //  ----------------------------------------------------------------------
            
            //  ----------------------------------------------------------------------
            //  BEGIN TEST 2 - Update with no conflict
            //  ----------------------------------------------------------------------
            try
                {
                FeedSyncItem = FeedSyncItemCollection1.GetItem("ep1.100");

                DescriptionXMLDOMNode = SelectSingleNode
                    (
                    FeedSyncItem.m_XMLDOMElement,
                    "description",
                    null
                    );
                    
                SetTextForXMLDOMNode(DescriptionXMLDOMNode, "Get milk, eggs and butter");

                FeedSyncItem.Update("EP1");

                FeedSyncItem = FeedSyncItemCollection1.GetItem("ep1.100");
                DescriptionXMLDOMNode = SelectSingleNode
                    (
                    FeedSyncItem.m_XMLDOMElement,
                    "description",
                    null
                    );
                    
                SetTextForXMLDOMNode(DescriptionXMLDOMNode, "Get milk, eggs, butter and bread");
                    
                FeedSyncItem.Update("EP2");

                if (IsFirefox())
                    {
                    var TestDivHTMLDOMElement = document.getElementById("Test2");
                    SetTextForHTMLDOMNode(TestDivHTMLDOMElement, GetXML(FeedSyncItemCollection1.m_XMLDOMDocument));
                    }
                else
                    {             
                    document.write("<div><b><u>Test 2 Results</u></b></div>");
                    document.write(FeedSyncItemCollection1.m_XMLDOMDocument.transformNode(XSLDOMDocument));
                    document.write("<br/>");
                    }
                }
            catch (e)
                {
                alert("Exception in Test 2 - " + e.message);
                }
            //  ----------------------------------------------------------------------
            //  END TEST 2
            //  ----------------------------------------------------------------------

            //  ----------------------------------------------------------------------
            //  BEGIN TEST 3 - Update with conflict
            //  ----------------------------------------------------------------------
            try
                {
                XMLDOMDocument2 = ParseXML(GetXML(FeedSyncItemCollection1.m_XMLDOMDocument), true);
                FeedSyncItemCollection2 = new FeedSyncItemCollectionClass(XMLDOMDocument2);

                FeedSyncItem = FeedSyncItemCollection1.GetItem("ep1.100");

                DescriptionXMLDOMNode = SelectSingleNode
                    (
                    FeedSyncItem.m_XMLDOMElement,
                    "description",
                    null
                    );
                    
                SetTextForXMLDOMNode(DescriptionXMLDOMNode, "Get milk, eggs, butter and rolls");

                FeedSyncItem.Update("EP1");

                FeedSyncItem = FeedSyncItemCollection2.GetItem("ep1.100");
                var TitleXMLDOMNode = SelectSingleNode
                    (
                    FeedSyncItem.m_XMLDOMElement,
                    "title",
                    null
                    );
                    
                SetTextForXMLDOMNode(TitleXMLDOMNode, "Buy Groceries - DONE");

                DescriptionXMLDOMNode = SelectSingleNode
                    (
                    FeedSyncItem.m_XMLDOMElement,
                    "description",
                    null
                    );
                    
                SetTextForXMLDOMNode(DescriptionXMLDOMNode, "Get milk, eggs, butter and bread");

                alert("Delay for Test 3");
                
                FeedSyncItem.Update("EP2");

                FeedSyncItemCollection1.MergeItems(FeedSyncItemCollection2);

                if (IsFirefox())
                    {
                    var TestDivHTMLDOMElement = document.getElementById("Test3");
                    SetTextForHTMLDOMNode(TestDivHTMLDOMElement, GetXML(FeedSyncItemCollection1.m_XMLDOMDocument));
                    }
                else
                    {             
                    document.write("<div><b><u>Test 3 Results</u></b></div>");
                    document.write(FeedSyncItemCollection1.m_XMLDOMDocument.transformNode(XSLDOMDocument));
                    document.write("<br/>");
                    }
                }
            catch (e)
                {
                alert("Exception in Test 3 - " + e.message);
                }
            //  ----------------------------------------------------------------------
            //  END TEST 3
            //  ----------------------------------------------------------------------

            //  ----------------------------------------------------------------------
            //  BEGIN TEST 4 - Resolve conflict
            //  ----------------------------------------------------------------------
            try
                {
                FeedSyncItem = FeedSyncItemCollection1.GetItem("ep1.100");
                var TitleXMLDOMNode = SelectSingleNode
                    (
                    FeedSyncItem.m_XMLDOMElement,
                    "title",
                    null
                    );
                    
                SetTextForXMLDOMNode(TitleXMLDOMNode, "Buy Groceries - DONE");

                DescriptionXMLDOMNode = SelectSingleNode
                    (
                    FeedSyncItem.m_XMLDOMElement,
                    "description",
                    null
                    );
                    
                SetTextForXMLDOMNode(DescriptionXMLDOMNode, "Get milk, eggs, butter and bread");

                FeedSyncItem.ResolveConflicts("EP2");

                if (IsFirefox())
                    {
                    var TestDivHTMLDOMElement = document.getElementById("Test4");
                    SetTextForHTMLDOMNode(TestDivHTMLDOMElement, GetXML(FeedSyncItemCollection1.m_XMLDOMDocument));
                    }
                else
                    {             
                    document.write("<div><b><u>Test 4 Results</u></b></div>");
                    document.write(FeedSyncItemCollection1.m_XMLDOMDocument.transformNode(XSLDOMDocument));
                    document.write("<br/>");
                    }
                }
            catch (e)
                {
                alert("Exception in Test 4 - " + e.message);
                }
            //  ----------------------------------------------------------------------
            //  END TEST 4
            //  ----------------------------------------------------------------------

            //  ----------------------------------------------------------------------
            //  BEGIN TEST 5 - Delete with no conflicts
            //  ----------------------------------------------------------------------
            try
                {
                XMLDOMDocument2 = ParseXML(GetXML(FeedSyncItemCollection1.m_XMLDOMDocument), true);
                FeedSyncItemCollection2 = new FeedSyncItemCollectionClass(XMLDOMDocument2);

                FeedSyncItem = FeedSyncItemCollection1.GetItem("ep1.101");
                FeedSyncItem.Delete("EP1");

                FeedSyncItem = FeedSyncItemCollection2.GetItem("ep2.101");
                FeedSyncItem.Delete("EP2");

                FeedSyncItemCollection1.MergeItems(FeedSyncItemCollection2);

                if (IsFirefox())
                    {
                    var TestDivHTMLDOMElement = document.getElementById("Test5");
                    SetTextForHTMLDOMNode(TestDivHTMLDOMElement, GetXML(FeedSyncItemCollection1.m_XMLDOMDocument));
                    }
                else
                    {             
                    document.write("<div><b><u>Test 5 Results</u></b></div>");
                    document.write(FeedSyncItemCollection1.m_XMLDOMDocument.transformNode(XSLDOMDocument));
                    document.write("<br/>");
                    }
                }
            catch (e)
                {
                alert("Exception in Test 5 - " + e.message);
                }
            //  ----------------------------------------------------------------------
            //  END TEST 5
            //  ----------------------------------------------------------------------

            //  ----------------------------------------------------------------------
            //  BEGIN TEST 6 - Delete with conflicts
            //  ----------------------------------------------------------------------
            try
                {
                XMLDOMDocument2 = ParseXML(GetXML(FeedSyncItemCollection1.m_XMLDOMDocument), true);
                FeedSyncItemCollection2 = new FeedSyncItemCollectionClass(XMLDOMDocument2);

                FeedSyncItem = FeedSyncItemCollection1.GetItem("ep2.100");
                FeedSyncItem.Delete("EP1");

                alert("Delay for Test 6");

                FeedSyncItem = FeedSyncItemCollection2.GetItem("ep2.100");                
                FeedSyncItem.Delete("EP2");

                FeedSyncItemCollection1.MergeItems(FeedSyncItemCollection2);

                if (IsFirefox())
                    {
                    var TestDivHTMLDOMElement = document.getElementById("Test6");
                    SetTextForHTMLDOMNode(TestDivHTMLDOMElement, GetXML(FeedSyncItemCollection1.m_XMLDOMDocument));
                    }
                else
                    {             
                    document.write("<div><b><u>Test 6 Results</u></b></div>");
                    document.write(FeedSyncItemCollection1.m_XMLDOMDocument.transformNode(XSLDOMDocument));
                    document.write("<br/>");
                    }
                }
            catch (e)
                {
                alert("Exception in Test 6 - " + e.message);
                }
            //  ----------------------------------------------------------------------
            //  END TEST 6
            //  ----------------------------------------------------------------------

            }
    </script>
</head>
<body onload="Initialize()">
    <div><b><u>Test1 Results</u></b><div id="Test1"/></div>
    <br />
    <div><b><u>Test2 Results</u></b><div id="Test2"/></div>
    <br />
    <div><b><u>Test3 Results</u></b><div id="Test3"/></div>
    <br />
    <div><b><u>Test4 Results</u></b><div id="Test4"/></div>
    <br />
    <div><b><u>Test5 Results</u></b><div id="Test5"/></div>
    <br />
    <div><b><u>Test6 Results</u></b><div id="Test6"/></div>
</body>
</html>
